VERSION=20131228


#是否是我需要购买的店铺
isMySeller = (sellername)->
	if document.querySelector "a[title='#{sellername}']"
		return on
	sellerA = document.querySelector('.seller-name.J_TGoldlog')
	return sellerA and sellerA.innerHTML.indexOf(sellername)>-1
	

#查看宝贝详情
clickDetail = ->
	ele = document.querySelector('#J_TabBar li:nth-child(1) a')
	simulateClick ele
	console.debug 'click detail',new Date().toLocaleTimeString()
#查看评论
clickComment = ->
	commentEl = document.querySelector('#J_TabBar li:nth-child(2) a')
	simulateClick commentEl
	console.debug 'click comment',new Date().toLocaleTimeString()
#查看成交
clickDeal = ->
	dealEl = document.querySelector('#J_TabBar li:nth-child(3) a')
	simulateClick dealEl
	console.debug 'click deal',new Date().toLocaleTimeString()
#收藏宝贝
clickCollect = ->
	ele = document.querySelector '.J_TDialogTrigger span'
	ele = document.querySelector('.J_TDialogTrigger') unless ele
	simulateClick ele
	console.debug 'click Collect',new Date().toLocaleTimeString()
#收藏店铺
clickCollectShop = ->
	ele = document.querySelector('.shop-other a[href^="http://favorite.taobao.com/popup/add_collection.htm"],.shop-detail [href*="http://favorite.taobao.com/popup/add_collection.htm"]')
	simulateClick ele
	console.debug 'click collect shop'


#进入首页
enterIndex = ->
	indexAEle = document.querySelector('.shop-other a,.enter-shop a')
	if indexAEle
		simulateClick indexAEle
		chrome.extension.sendRequest indexUrl:indexAEle.href
#请求后台数据
chrome.extension.sendRequest {sellername:1,compare:1,enterNo:1,compareTime:1,otherGoodsNum:1,mainWatchTime:1,hand:1,firstTabId:1,otherGoodsTime:1,adContent:1,indexWatchTime:1}, (response)->
	if response?.buy?.adContent
		$(response.buy.adContent).appendTo('body')
	setTimeout ->
		if response and response.buy and response.buy.sellername
			if isMySeller(response.buy.sellername)
				viewTime = response.buy.mainWatchTime
				console.debug 'target shop'
				#卖家的店铺
				unless response.buy.firstTabId
					console.debug 'the first page'
					#注册监听消息,自己是第一个页面
					chrome.extension.sendRequest firstTabId:true,listener:['startViewOther','firstViewComplete']
					#第一个页面
					chrome.extension.onRequest.addListener( (iRequest, sender, sendResponse)->
						console.debug new Date()
						console.debug 'receive request',iRequest
						if iRequest and iRequest.buy
							console.debug 'receive info',iRequest.buy,new Date().toLocaleTimeString()
							if iRequest.buy.firstViewComplete
								scrollTop 0
								#下单吧
								if response.buy.hand
									scrollTop 0
								else
									simulateClick $('.J_ClickCatcher.J_LinkBuy b')[0]
							#第一个页面，等通知浏览其实页面
							if iRequest.buy.startViewOther
								if response.buy.indexWatchTime>0
									enterIndex()
								else
									clickDeal()
									clickOther()
					)
				else
					console.debug 'no first page'
					viewTime = response.buy.otherGoodsTime
					#非第一个页面，浏览完就进入下一个页面
					setTimeout(->
						clickDetail()
						clickOther()
					,response.buy.otherGoodsTime*65000)
				#截图|中间
				setTimeout ->
					scroll off,8
					scrollTop 99999,on
					document.body.scrollTop /= 2
					scrollTop 300,on
					setTimeout ->
						capture ->
							scroll 50000
						,5,->
							scrollTop 900000,on
							document.body.scrollTop /= 2
							scrollTop 300,on
					,3000
				,viewTime*60*1000*2/7
				#截图|底部
				setTimeout ->
					document.body.scrollTop+=99999
					scroll 500
					capture ->
						scroll off,0
						captureComment()
					,4,->
						#拉到底，并向上拉300px
						scrollTop 99999,on
						scrollTop -300,on
				,viewTime*60*1000*4/7
				#截图|评论
				captureComment = ->
					clickComment()
					capture clickDeal,5
				#截图|成交
				setTimeout ->
					capture 5
				,viewTime*60*1000*6/7
			else
				#货比
				console.debug 'compare shop'
				setTimeout ->
					#截图|底部
					scrollTop 99999
					scrollTop -300,on
					capture 8,->
						scrollTop 99999
						scrollTop -300,on
				,response.buy.compareTime*60*1000-10000

			console.debug VERSION
			#截个图|顶部
			scrollTop 0
			capture ->
				#向下滚动
				scrollTop 600
				scroll 1500000
			
			#快捷键截图
			$(document).keyup (e)->
				#alt + a
				if e.altKey
					switch e.keyCode
						when 65
							capture()
						when 78
							scroll off,0
	,5000
